﻿
using Iot.Device.Sht3x;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Device.Gpio;
using System.Device.I2c;
using System.Text;
using System.Threading;
using System.Linq;
using Models.Common;
using Models.Device;
using IotDeviceToolHepler;
using Iot.Device.Nmea0183.Sentences;
using IotDeviceToolHepler.Model.Device;

using Models.WebClient;
using IotDeviceToolHepler.DeviceForRaspberryPi;
using UtilsBase;
using UtilsCache;
using GatewayClient.Common;

namespace GatewayClient.Device
{
    public static class ServiceDeviceLightLM393SearchService
    {
        public static void getLight()
        {

            //new UtilsFileHelper().log_WriteMessageAppend("LightLM393SearchService", "getLight运行");
            try
            {
                HttpReqModel reqIotModel = new HttpReqModel();
                reqIotModel.serverDeviceId = CommonMachineInfo.getMachineId();
                RespIotModel respIotModel = getConfigure(reqIotModel);
                if (respIotModel == null) return;
                //-----------------------------------------------
                while (true)
                {
                    ModelLightLM393AoInfo lM393AoReturnValue = Sensor_LightLM393Ao.getData();
                    //-----------------------推数据
                    reqIotModel.lightState = lM393AoReturnValue.lightState;
                    reqIotModel.lightNumber = lM393AoReturnValue.lightAo;//数值越大，越黑，越小越亮
                    reqIotModel.deviceId = respIotModel.i2cAddress; ;
                    reqIotModel.gpioNum = respIotModel.gpioNum;
                    reqIotModel.i2cAddress = respIotModel.i2cAddress;
                    pushData(reqIotModel);
                    Thread.Sleep(10000);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                // new UtilsFileHelper().log_WriteMessageAppend("error", ex.ToString());
            }
        }
        /// <summary>
        /// 获取配置
        /// </summary>
        /// <param name="reqIotModel"></param>
        /// <returns></returns>
        private static RespIotModel getConfigure(HttpReqModel reqIotModel)
        {
            //请求接口
            UtilsHttpHelper http = new UtilsHttpHelper();
            //读取apiurl并更新
            var jsonuserApiUrl = http.sendPostRequest(UtilAppsettingCache.getSetting("lightSensor:getLightConfigure"), "jsonStr=" + JsonConvert.SerializeObject(reqIotModel));
            Console.WriteLine(JsonConvert.SerializeObject(reqIotModel));


            if (jsonuserApiUrl != null)
            {
                MQReqModel<List<RespIotModel>> datauser = JsonConvert.DeserializeObject<MQReqModel<List<RespIotModel>>>(jsonuserApiUrl);
                if (datauser.data == null) return null;

                //获取第一条，如果有多个传感器 这里要修改
                return datauser.data.FirstOrDefault();
            }
            return null;
        }

        private static void pushData(HttpReqModel reqIotModel)
        {
            //请求接口
            UtilsHttpHelper http = new UtilsHttpHelper();
            //读取apiurl并更新
            var jsonuserApiUrl = http.sendPostRequest(UtilAppsettingCache.getSetting("lightSensor:pushLightDeviceData"), "jsonStr=" + JsonConvert.SerializeObject(reqIotModel));
            Console.WriteLine(JsonConvert.SerializeObject(reqIotModel));
            Console.WriteLine(jsonuserApiUrl);
        }


    }
}


